package atm05;

import atm04.FileUtil;
import atm04.Users;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener{
    private ATM05 atm05;
    private JLabel otherUserLabel = new JLabel("输入转账用户名：");
    private JLabel otherBlance = new JLabel("输入转账金额：");
    private JTextField userText = new JTextField();
    private JTextField blanceText = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("src/atm05/Images/buttonSure.jpg"));
    private JButton exitBtn = new JButton(new ImageIcon("src/atm05/Images/buttonOff.jpg"));
    private Font font = new Font("黑体",Font.BOLD,25);

    public TransferPanel(ATM05 atm05) {
        this.atm05 = atm05;
        this.setLayout(null);

        otherUserLabel.setBounds(300,200,220,50);
        otherUserLabel.setFont(font);

        otherBlance.setBounds(300,300,220,50);
        otherBlance.setFont(font);

        userText.setBounds(530,200,220,50);
        userText.setFont(font);

        blanceText.setBounds(530,300,220,50);
        blanceText.setFont(font);

        sureBtn.setBounds(420,420,140,50);
        sureBtn.addActionListener(this);

        exitBtn.setBounds(880,380,140,50);
        exitBtn.addActionListener(this);


        this.add(otherUserLabel);
        this.add(otherBlance);
        this.add(userText);
        this.add(blanceText);
        this.add(sureBtn);
        this.add(exitBtn);

        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            if (e.getSource() == sureBtn){
                String otherUser = userText.getText();
                double blance = Double.parseDouble(blanceText.getText());
                Users loginUser = atm05.getLoginUser();
                if (loginUser.getAccount().equals(otherUser)){
                    JOptionPane.showMessageDialog(this,"不能转给自己");
                    return;
                }
                boolean flag = false;
                int oIndex = 0;
                for (int i = 0; i < atm05.getUsers().size(); i++){
                    if (otherUser.equals(atm05.getUsers().get(i).getAccount())){
                        flag = true;
                        oIndex = i;
                        break;
                    }
                }
                if (!flag){
                    JOptionPane.showMessageDialog(this,"账号不存在");
                    return;
                }
                if (loginUser.getBlance() < blance){
                    JOptionPane.showMessageDialog(this,"余额不足");
                    return;
                }

                loginUser.setBlance(loginUser.getBlance() - blance);
                atm05.getUsers().get(oIndex).setBlance(atm05.getUsers().get(oIndex).getBlance() + blance);
                FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
                JOptionPane.showMessageDialog(this,"转账成功");
            }else if (e.getSource() == exitBtn){
                userText.setText("");
                blanceText.setText("");
                atm05.showPanel("menu");
            }
        }catch (Exception e1){
            JOptionPane.showMessageDialog(this,"您的输入有误，请重新输入");
            userText.setText("");
            blanceText.setText("");
        }

    }
}
